package map_set.leetcode.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bruin_du
 * @description 外星语言是否排序
 * @date 2022/8/12 21:08
 **/
public class OfferII034_IsAlienSorted {
    public boolean isAlienSorted(String[] words, String order) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < order.length(); i++) {
            map.put(order.charAt(i), i);
        }
        for (int i = 0; i < words.length - 1; i++) {
            String str1 = words[i];
            String str2 = words[i + 1];
            int j = 0;
            int len = Math.min(str1.length(), str2.length());
            for (; j < len; j++) {
                int x1 = map.get(str1.charAt(j));
                int x2 = map.get(str2.charAt(j));
                if (x1 > x2) return false;
                if (x1 < x2) break;
            }
            if (j == str2.length() && j < str1.length()) return false;
        }
        return true;
    }
}
